package com.icss.clothes.service.imp;

import com.icss.clothes.entity.Buy;
import com.icss.clothes.entity.Order_detail;
import com.icss.clothes.entity.Orders;
import com.icss.clothes.entity.Shopdetail;
import com.icss.clothes.mapper.BuyMapper;
import com.icss.clothes.mapper.OrderMapper;
import com.icss.clothes.mapper.ShopDetailMapper;
import com.icss.clothes.service.IOrderService;
import com.icss.clothes.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    @Autowired
    private BuyMapper buyMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Orders findAllByOrder_id(String order_id) {
        //从数据库中通过指定的订单编号查询得到所有订单信息
        return orderMapper.selectAllByOrder_id(order_id);
    }

    @Override
    public void generateOrder(Orders orders, Order_detail orderDetail) {
        //向订单表中插入一条指定的订单信息
        orderMapper.insertIntoByOrders(orders);
        //向订单详情表中插入一条指定的订单详情信息
        orderMapper.insertIntoByOrder_detail(orderDetail);
        //从数据库中修改指定的商品库存
        shopDetailMapper.updateC_numberByCd_id(orderDetail.getCd_id(),orderDetail.getOd_number());
    }

    @Override
    public String generateOrders(int[] car_id, double oprice, int uid) {
        //需要自己生成一个订单编号
        //向订单表中插入一条订单信息
        Orders orders = new Orders();
        String order_id= RandomUtil.generateOid();
        orders.setOrder_id(order_id);
        orders.setUser_id(uid);
        orders.setOrder_price(oprice);
        orders.setOrder_state(0);
        orderMapper.insertIntoByOrders(orders);
        //前提：循环遍历购物车编号，通过指定的购物车编号查询购物车信息——》向订单详情表中插入记录、修改指定改商品的库存
        for (int id:car_id){
            //通过指定的购物车编号查询购物车信息
            Buy buy=buyMapper.selectAllByCar_id(id);
            //向订单详情表中插入记录
            Order_detail orderDetail=new Order_detail();
            orderDetail.setOrder_id(order_id);
            orderDetail.setCd_id(buy.getCd_id());
            orderDetail.setC_id(buy.getC_id());
            orderDetail.setOd_number(buy.getQuantity());
            orderMapper.insertIntoByOrder_detail(orderDetail);
            //修改指定商品的库存
            shopDetailMapper.updateC_numberByCd_id(buy.getCd_id(),buy.getQuantity());
            //通过指定的购物车编号删除购物车信息
            buyMapper.deleteCarByCar_id(id);
        }
        //返回该订单编号
        return order_id;
    }

    @Override
    public List<Orders> findAllByUser_id(int user_id) {
        //从数据库中通过指定的用户编号查询得到所有订单信息
        return orderMapper.selectAllByUser_id(user_id);
    }

    @Override
    public List<Order_detail> findOrderDetailAllByOrder_id(String order_id) {
        //从数据库中通过指定的订单编号查询得到商品详情信息
        return orderMapper.selectOrderDetailByOrder_id(order_id);
    }

    @Override
    public void deleteOrder(String order_id) {
        //从数据库中通过指定的订单编号查询得到所有订单详情信息
        List<Order_detail> orderDetails=orderMapper.selectOrderDetailByOrder_id(order_id);
        //循环遍历得到的数据,将redis缓存数据库和MySQL数据库中的库存信息修改,同时删除指定的订单编号的订单信息和订单详情信息
        for (Order_detail order:orderDetails){
            //修改redis缓存数据库中的库存信息
            redisTemplate.opsForValue().increment("ac:"+order.getCd_id(),order.getOd_number());
            //从数据库中通过指定的商品编号修改指定的商品库存
            shopDetailMapper.updateNumberByCd_id(order.getCd_id(),order.getOd_number());
        }
        //通过指定的订单编号删除订单表中的订单信息
        orderMapper.deleteOrderByOrder_id(order_id);
        //通过指定的订单编号删除订单详情表中的订单信息
        orderMapper.deleteOrderDetailByOrder_id(order_id);
    }

    @Override
    public void updateAddressByOrder_id(String order_id, int address_id) {
        //从数据库中修改指定的订单的收货地址
        orderMapper.updateAddressByOrder_id(order_id,address_id);
    }

    @Override
    public void updateStateByOrder_id(String order_id, int order_state) {
        //从数据库中通过指定的订单编号修改指定的订单状态
        orderMapper.updateStateByOrder_id(order_id,order_state);
    }
}
